package com.huoniu.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huoniu.api.entity.User;
import com.huoniu.api.entity.dto.UserKeyWordDTO;
import com.huoniu.api.entity.vo.UserVO;
import com.huoniu.api.service.UserService;
import com.huoniu.common.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @类 名： UserController <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/5 10:30<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public Result insert(@RequestBody User user) {
        if (userService.insert(user)) {
            return Result.success("用户添加成功");
        }
        return Result.fail(user.getUsername() + "已存在！");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        if (userService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Long id) {
        User user = userService.getUserById(id);
        if (null != user) {
            return Result.success(user);
        }
        return Result.fail("id错误!");
    }
    @GetMapping("/info/{username}")
    public Result getUserByUsername(@PathVariable("username") String username) {
        User user = userService.getUserByUsername(username);
        if (null != user) {
            return Result.success(user);
        }
        return Result.fail("id错误!");
    }
    @PutMapping
    public Result update(@RequestBody User user) {
        if (userService.update(user)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    /**
     * 总记录
     * 页大小
     * 当前页
     *
     * @param userKeyWordDTO
     * @return
     */
    @PostMapping("/list")
    public Result getUsers(@RequestBody UserKeyWordDTO userKeyWordDTO) {
//        IPage<User> userIPage=userService.getUsers(userKeyWordDTO);
        IPage<UserVO> userIPage = userService.getUserList(userKeyWordDTO);
        return Result.success(userIPage);
    }
}
